package idv.arthur.work;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

/**
 * Servlet implementation class RoleList
 */
//@WebServlet(description = "暨有角色清單", urlPatterns = { "/RoleList" })
public class RoleList extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoleList() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Widgets wid = new Widgets();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String strSQL = "SELECT roleID,roleName FROM tms_role";
		String returnResult = "";
		ArrayList<RoleLists> roleLists = new ArrayList<RoleLists>();
		
		try {
			conn = wid.getConn(true);
			ps = conn.prepareStatement(strSQL);
			rs = ps.executeQuery();
			
			while (rs.next()){
				RoleLists RoleList = new RoleLists();
				RoleList.setRoleID(rs.getString("roleID"));
				RoleList.setRoleName(rs.getString("roleName"));
				ArrayList<User> users = new ArrayList<User>();
				
				strSQL = "SELECT t2.userID, t2.nickName FROM tms_roleusermapping t1, tms_personnel t2 WHERE t1.userID = t2.userID AND t1.roleID=?";
				ps = conn.prepareStatement(strSQL);
				ps.setString(1, rs.getString("roleID"));
				rs2 = ps.executeQuery();
				while (rs2.next()) {
					User user = new User();
					user.setUserID(rs2.getString("userID"));
					user.setNickName(rs2.getString("nickName"));
					users.add(user);
				}
				RoleList.setUsers(users);
				roleLists.add(RoleList);
			}
			
			returnResult = new Gson().toJson(roleLists);
		} catch (SQLException e) {
			returnResult = "{\"errorMsg\":\""+wid.keepErrorMes(conn, ps, e, "", true)+"\"}";
		} finally {
			try {
				if (rs!= null) {rs.close();}
				if (rs2!= null) {rs2.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) {
				returnResult = e.toString();
			}
		}
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write(returnResult);
	}
}

class RoleLists {
	private String roleID;
	private String roleName;
	private ArrayList<User> users;
	
	public String getRoleID() {
		return roleID;
	}
	public void setRoleID(String roleID) {
		this.roleID = roleID;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public ArrayList<User> getUsers() {
		return users;
	}
	public void setUsers(ArrayList<User> users) {
		this.users = users;
	}
}

